package com.kaiwukj.android.ufamily.mvp.ui.page.dynamic;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseActivity;
import com.kaiwukj.android.ufamily.app.constant.ARouterUrlKt;
import com.kaiwukj.android.ufamily.app.constant.ExtraCons;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.DynamicResult;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.detail.DynamicDetailFragment;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.publish.DynamicPublishFragment;
import com.kaiwukj.android.ufamily.mvp.ui.page.dynamic.report.ReportCardFragment;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;


/**
 * Copyright © KaiWu Technology Company
 *
 * @author Haife
 * @job Android Development
 * @company KW | 开物科技
 * @time 2019/7/19
 * @desc
 */
@Route(path = ARouterUrlKt.SocialCircleUrl)
public class SocialCircleActivity extends BaseActivity {
    @Autowired(name = FRAGMENT_KEY)
    String mFragmentKey;
    @Autowired(name = FRAGMENT_KEY_CARD_BEAN)
    DynamicResult dynamic;
    @Autowired
    int dynamicId;
    @Autowired
    DynamicResult dynamicResult;
    @Autowired(name = ExtraCons.EXTRA_KEY_USER_ID)
    int mUserId;
    @Autowired(name = ExtraCons.EXTRA_KEY_ORDER_MINE_INDEX)
    int index;
    @Autowired
    String media;//List<LocalMedia> toJson
    public static final String FRAGMENT_KEY = "FRAGMENT_KEY";

    public static final String FRAGMENT_KEY_CARD_BEAN = "FRAGMENT_KEY_CARD_BEAN";

    @Override
    protected int getLayoutId() {
        ARouter.getInstance().inject(this);
        return R.layout.activity_social_circle;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        if (DynamicDetailFragment.CIRCLE_CARD_DETAIL.equals(mFragmentKey)) {
            loadRootFragment(R.id.fl_social_circle_container, DynamicDetailFragment.newInstance(dynamicId));
        } else if (DynamicDetailFragment.CIRCLE_CARD_DETAIL_ID.equals(mFragmentKey)) {
            loadRootFragment(R.id.fl_social_circle_container, DynamicDetailFragment.newInstance(dynamicId));
        } else if (DynamicPublishFragment.POST_CARD_TOPIC_FRAGMENT.equals(mFragmentKey)) {
            List<LocalMedia> results = null;
            if (!StringUtils.isEmpty(media)) {
                results = GsonUtils.fromJson(media, GsonUtils.getListType(LocalMedia.class));
            }
            loadRootFragment(R.id.fl_social_circle_container, DynamicPublishFragment.newInstance(results));
        } else if (ReportCardFragment.REPORT_PAGE.equals(mFragmentKey)) {
            loadRootFragment(R.id.fl_social_circle_container, ReportCardFragment.newInstance(dynamicResult));
        }

    }


    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return new DefaultHorizontalAnimator();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        FragmentManager fragmentManager = getSupportFragmentManager();
        for (int indext = 0; indext < fragmentManager.getFragments().size(); indext++) {
            Fragment fragment = fragmentManager.getFragments().get(indext); //找到第一层Fragment
            if (fragment == null)
                LogUtils.w("Activity result no fragment exists for index: 0x"
                        + Integer.toHexString(requestCode));
            else
                handleResult(fragment, requestCode, resultCode, data);
        }
    }

    /**
     * 递归调用，对所有的子Fragment生效
     *
     * @param fragment
     * @param requestCode
     * @param resultCode
     * @param data
     */
    private void handleResult(Fragment fragment, int requestCode, int resultCode, Intent data) {
        fragment.onActivityResult(requestCode, resultCode, data);//调用每个Fragment的onActivityResult
        List<Fragment> childFragment = fragment.getChildFragmentManager().getFragments(); //找到第二层Fragment
        for (Fragment f : childFragment)
            if (f != null) {
                handleResult(f, requestCode, resultCode, data);
            }
    }
}
